package com.tianqu.tms.www.transfer.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.CopyFile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.util.UrlBase64Coder;
import com.tianqu.tms.core.util.car.TmsCarModelInfoApi;
import com.tianqu.tms.core.webservice.weichat.api.WeChatMqApi;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn000431Bo;
import com.tianqu.tms.dao.CityDaoImp;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TransferProcessHistoryDaoImp;
import com.tianqu.tms.dto.BaseCarInfoDto;
import com.tianqu.tms.dto.CityDto;
import com.tianqu.tms.dto.MarketCarInfoDto;
import com.tianqu.tms.dto.TransferAcceptedInfoDto;
import com.tianqu.tms.dto.TransferProcessHistoryDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

import jcifs.smb.SmbFile;

public class CarRgInputConfirmController extends TmsController {
	private Logger logger = Logger.getLogger(CarRgInputConfirmController.class);

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
		
			TransferRgActionForm inputForm = (TransferRgActionForm) uc.getInputForm();

			// print save action
			if ("1".equals(inputForm.getSaveInputInfo()) || "2".equals(inputForm.getSaveInputInfo())) {
				controller.begin();
				TransferAcceptedInfo transferAcceptedInfo = new TransferAcceptedInfo();
				transferAcceptedInfo.updateAcceptInfo4TransferConfirmBySerial(convert(inputForm, uc));
				//
				// 插入 过户流程操作记录表（tms_transfer_process_history）
				TransferProcessHistoryDto transferProcessHistoryDto = new TransferProcessHistoryDto();
				if ("1".equals(inputForm.getSaveInputInfo()))
					transferProcessHistoryDto.setBusinessType(6);
				else
					transferProcessHistoryDto.setBusinessType(5);
				TmsUserProfile profile = uc.getUserProfile();
				transferProcessHistoryDto.setInsertUserId(profile.getUniqueId());
				transferProcessHistoryDto.setSerial(inputForm.getSerial());
				TransferProcessHistoryDaoImp transferProcessHistoryDao = (TransferProcessHistoryDaoImp) TransferProcessHistoryDaoImp.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
				transferProcessHistoryDao.insertHisInfoBySerial(transferProcessHistoryDto);
				//

				if ("1".equals(inputForm.getSaveInputInfo())) {// print
					Car car = new Car();
					car = car.findCarBaseInfoByVin(inputForm.getVin());
					BaseCarInfoDto baseCarInfoDto = new BaseCarInfoDto();
					baseCarInfoDto.setVin(inputForm.getVin());
					if (StringUtil.isNotBlank(inputForm.getCountryStyle())) 	baseCarInfoDto.setCountryStyle(Integer.parseInt(inputForm.getCountryStyle()));
					if (StringUtil.isNotBlank(inputForm.getCarUse())) 	baseCarInfoDto.setCarUse(Integer.parseInt(inputForm.getCarUse()));
					if (StringUtil.isNotBlank(inputForm.getCardType())) 	baseCarInfoDto.setCardType(Integer.parseInt(inputForm.getCardType()));
					baseCarInfoDto.setRegisterCardNo(inputForm.getRegisterCardNo());
					baseCarInfoDto.setCarOwner(inputForm.getCarOwner());
					baseCarInfoDto.setOwnerAddress(inputForm.getOwnerAddress());
					baseCarInfoDto.setOwnerPhone(inputForm.getOwnerPhone());
					baseCarInfoDto.setCarOwnerIdCardNo(inputForm.getCarOwnerIdCardNo());
					baseCarInfoDto.setModelCode(inputForm.getModelCode());
					baseCarInfoDto.setVendorCode(inputForm.getVendorCode());
					baseCarInfoDto.setCarOwnerIdCardNo(inputForm.getCarOwnerIdCardNo());
					if (StringUtil.isNotBlank(inputForm.getCarType())) 	baseCarInfoDto.setCarType(Integer.parseInt(inputForm.getCarType()));
					if (StringUtil.isNotBlank(inputForm.getCarOwner())) {
						if (inputForm.getCarOwner().length() >= 5) baseCarInfoDto.setOwnerType(2);
						else baseCarInfoDto.setOwnerType(1);
					}
					baseCarInfoDto.setCarOwnerIdCardNo(inputForm.getIdentification());
					TransferAcceptedInfo info = TransferAcceptedInfo.getInstance().findAcceptInfoByVin(inputForm.getVin());
					if (info != null && info.getIsMatch() == 2) {
						baseCarInfoDto.setCarName(info.getCarName());
						baseCarInfoDto.setVendorId(info.getVendorId());
						baseCarInfoDto.setBrandId(info.getBrandId());
						baseCarInfoDto.setCarStyleId(info.getCarStyleId());
					}
					if (car == null) {// insert
						Car.getInstance().insertBaseCarInfo(baseCarInfoDto);
					} else {// update
						Car.getInstance().updateBaseCarInfo(baseCarInfoDto);
					}

					//
					if (StringUtil.isNotBlank(inputForm.getIdentification())) {
						MarketCarInfoDto marketCarInfoDto = new MarketCarInfoDto();
						marketCarInfoDto.setCarOwner(inputForm.getCarOwner());
						marketCarInfoDto.setOwnerPhone(inputForm.getOwnerPhone());
						marketCarInfoDto.setOwnerAddress(inputForm.getOwnerAddress());
						marketCarInfoDto.setOwnerCardNo(inputForm.getIdentification());
						if (StringUtil.isNotBlank(inputForm.getCarOwner())) {
							if (inputForm.getCarOwner().length() >= 5) marketCarInfoDto.setOwnerType(2);
							else marketCarInfoDto.setOwnerType(1);
						}
						if (StringUtil.isNotBlank(inputForm.getCardType()))
							marketCarInfoDto.setCardType(Integer.parseInt(inputForm.getCardType()));
						List<MarketCarInfoDto> list = Car.getInstance().findCarOwnerByCardNo(inputForm.getIdentification());
						if (list.size() == 0) {// insert
							Car.getInstance().insertCarOwner(marketCarInfoDto);
						} else {
							Car.getInstance().updateCarOwnerByOwnerCardNo(marketCarInfoDto);
						}
					}

					if (StringUtil.isNotBlank(inputForm.getAgentOwnerIdCardNo())) {
						MarketCarInfoDto marketCarInfoDto = new MarketCarInfoDto();
						marketCarInfoDto.setCarOwner(inputForm.getAgentName());
						marketCarInfoDto.setOwnerPhone(inputForm.getAgentPhone());
						marketCarInfoDto.setOwnerAddress(inputForm.getAgentAddress());
						marketCarInfoDto.setOwnerCardNo(inputForm.getAgentOwnerIdCardNo());
						marketCarInfoDto.setOwnerType(1);
						if (StringUtil.isNotBlank(inputForm.getCardType()))
							marketCarInfoDto.setCardType(Integer.parseInt(inputForm.getCardType()));
						List<MarketCarInfoDto> list = Car.getInstance().findCarOwnerByCardNo(inputForm.getAgentOwnerIdCardNo());
						if (list.size() == 0) {// insert
							Car.getInstance().insertCarOwner(marketCarInfoDto);
						} else {
							Car.getInstance().updateCarOwnerByOwnerCardNo(marketCarInfoDto);
						}
					}

					if (StringUtil.isNotBlank(inputForm.getNewIdentification())) {
						MarketCarInfoDto marketCarInfoDto = new MarketCarInfoDto();
						marketCarInfoDto.setCarOwner(inputForm.getNewCarOwner());
						marketCarInfoDto.setOwnerPhone(inputForm.getNewOwnerPhone());
						marketCarInfoDto.setOwnerAddress(inputForm.getNewOwnerAddress());
						marketCarInfoDto.setOwnerCardNo(inputForm.getNewIdentification());
						if (StringUtil.isNotBlank(inputForm.getCardType()))
							marketCarInfoDto.setCardType(Integer.parseInt(inputForm.getCardType()));
						if (StringUtil.isNotBlank(inputForm.getNewCarOwner())) {
							if (inputForm.getNewCarOwner().length() >= 5) marketCarInfoDto.setOwnerType(2);
							else marketCarInfoDto.setOwnerType(1);
						}
						List<MarketCarInfoDto> list = Car.getInstance().findCarOwnerByCardNo(inputForm.getNewIdentification());
						if (list.size() == 0) {// insert
							Car.getInstance().insertCarOwner(marketCarInfoDto);
						} else {
							Car.getInstance().updateCarOwnerByOwnerCardNo(marketCarInfoDto);
						}
					}

					if (StringUtil.isNotBlank(inputForm.getNewAgentOwnerIdCardNo())) {
						MarketCarInfoDto marketCarInfoDto = new MarketCarInfoDto();
						marketCarInfoDto.setCarOwner(inputForm.getNewAgentName());
						marketCarInfoDto.setOwnerPhone(inputForm.getNewAgentPhone());
						marketCarInfoDto.setOwnerAddress(inputForm.getNewAgentAddress());
						marketCarInfoDto.setOwnerCardNo(inputForm.getNewAgentOwnerIdCardNo());
						marketCarInfoDto.setOwnerType(1);
						if (StringUtil.isNotBlank(inputForm.getCardType()))
							marketCarInfoDto.setCardType(Integer.parseInt(inputForm.getCardType()));
						List<MarketCarInfoDto> list = Car.getInstance().findCarOwnerByCardNo(inputForm.getNewAgentOwnerIdCardNo());
						if (list.size() == 0) {// insert
							Car.getInstance().insertCarOwner(marketCarInfoDto);
						} else {
							Car.getInstance().updateCarOwnerByOwnerCardNo(marketCarInfoDto);
						}
					}
					
					if (StringUtil.isNotBlank(inputForm.getVin())) {
						List<MarketCarInfoDto> list = Car.getInstance().findCarListByVin(inputForm.getVin());
						if(list.size() > 0) {
							MarketCarInfoDto dto = list.get(0);
							Car.getInstance().updateCarByVin(dto.getVin(), inputForm.getRegDate());
						}
					}
				}

				controller.commit();

				// 431 API
				if ("1".equals(inputForm.getSaveInputInfo())) {
					TransferDataSyn000431Bo bo = new TransferDataSyn000431Bo();
					bo.setStatus(inputForm.getStatus() != null ? Integer.parseInt(inputForm.getStatus()) : null);
					bo.setSerial(inputForm.getSerial());
					bo.setVin(inputForm.getVin());
					bo.setVendorCode(inputForm.getVendorCode());
					bo.setModelCode(inputForm.getModelCode());
					if (StringUtil.isInteger(inputForm.getCarType())) bo.setCarType(Integer.parseInt(inputForm.getCarType()));
					if (StringUtil.isInteger(inputForm.getCountryStyle())) bo.setCountryStyle(Integer.parseInt(inputForm.getCountryStyle()));
					if (StringUtil.isInteger(inputForm.getCarUse())) bo.setCarUse(Integer.parseInt(inputForm.getCarUse()));
					if (StringUtil.isInteger(inputForm.getCardType())) 	bo.setCardType(Integer.parseInt(inputForm.getCardType()));
					bo.setRegiesterCardNo(inputForm.getRegisterCardNo());
					bo.setRegDate(inputForm.getRegDate());
					if (StringUtil.isInteger(inputForm.getGainType())) bo.setGainType(Integer.parseInt(inputForm.getGainType()));
					if (StringUtil.isInteger(inputForm.getCarMarketIn())) 	bo.setCarMarketInCity(Integer.parseInt(inputForm.getCarMarketIn()));
					CityDaoImp cdi = (CityDaoImp) TmsDao.getInstance(TmsDao.TMS_CITY);
					List<CityDto> list = cdi.queryCity(inputForm.getCarMarketIn());
					if (list != null && list.size() > 0) {
						CityDto city = list.get(0);
						if (StringUtil.isInteger(city.getProvinceId())) bo.setCarMarketInProv(Integer.parseInt(city.getProvinceId()));
					}
					bo.setCatClass(inputForm.getFromType());

					if (StringUtil.isInteger(inputForm.getPrice()))
						bo.setPrice(Integer.parseInt(inputForm.getPrice()));
					if (StringUtil.isInteger(inputForm.getOwnerType()))
						bo.setOwnerType(Integer.parseInt(inputForm.getOwnerType()));
					bo.setOwnerOrgNo(inputForm.getOwnerOrgNo());
					bo.setCarOwnerIdCardNo(inputForm.getCarOwnerIdCardNo());
					bo.setCarOwner(inputForm.getCarOwner());
					bo.setOwnerPhone(inputForm.getOwnerPhone());
					bo.setOwnerAddress(inputForm.getOwnerAddress());
					bo.setAgentOwnerIdCardNo(inputForm.getAgentOwnerIdCardNo());
					bo.setAgentName(inputForm.getAgentName());
					bo.setAgentPhone(inputForm.getAgentPhone());
					bo.setAgentAddress(inputForm.getAgentAddress());
					if (StringUtil.isInteger(inputForm.getNewOwnerType()))
						bo.setNewOwnerType(Integer.parseInt(inputForm.getNewOwnerType()));
					bo.setNewOwnerOrgNo(inputForm.getNewOwnerOrgNo());
					bo.setNewCarOwnerIdCardNo(inputForm.getNewcarOwnerIdCardNo());
					bo.setNewCarOwner(inputForm.getNewCarOwner());
					bo.setNewOwnerPhone(inputForm.getNewOwnerPhone());
					bo.setNewOwnerAddress(inputForm.getNewOwnerAddress());
					bo.setNewAgentOwnerIdCardNo(inputForm.getNewAgentOwnerIdCardNo());
					bo.setNewAgentName(inputForm.getNewAgentName());
					bo.setNewAgentPhone(inputForm.getNewAgentPhone());
					bo.setNewAgentAddress(inputForm.getNewAgentAddress());
					 
					String imagePath = ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.WECHAT_PIC_URL_KEY));
					String photo1 = !StringUtil.isNullOrBlank(inputForm.getMaterialImages1())?ImageUtils.transPicToStr(imagePath + getImageName(inputForm.getMaterialImages1())):"";
					String photo2 = !StringUtil.isNullOrBlank(inputForm.getMaterialImages2())?ImageUtils.transPicToStr(imagePath + getImageName(inputForm.getMaterialImages2())):"";
					String photo3 = !StringUtil.isNullOrBlank(inputForm.getMaterialImages3())?ImageUtils.transPicToStr(imagePath + getImageName(inputForm.getMaterialImages3())):"";
					String photo4 = !StringUtil.isNullOrBlank(inputForm.getMaterialImages4())?ImageUtils.transPicToStr(imagePath + getImageName(inputForm.getMaterialImages4())):"";
					String photo5 = !StringUtil.isNullOrBlank(inputForm.getMaterialImages5())?ImageUtils.transPicToStr(imagePath + getImageName(inputForm.getMaterialImages5())):"";
					String photo6 = !StringUtil.isNullOrBlank(inputForm.getMaterialImages6())?ImageUtils.transPicToStr(imagePath + getImageName(inputForm.getMaterialImages6())):"";
					String photo7 = !StringUtil.isNullOrBlank(inputForm.getMaterialImages7())?ImageUtils.transPicToStr(imagePath + getImageName(inputForm.getMaterialImages7())):"";
					String photo8 = !StringUtil.isNullOrBlank(inputForm.getMaterialImages8())?ImageUtils.transPicToStr(imagePath + getImageName(inputForm.getMaterialImages8())):"";
					String photo9 = !StringUtil.isNullOrBlank(inputForm.getMaterialImages9())?ImageUtils.transPicToStr(imagePath + getImageName(inputForm.getMaterialImages9())):"";
					String photo10 = !StringUtil.isNullOrBlank(inputForm.getMaterialImages10())?ImageUtils.transPicToStr(imagePath + getImageName(inputForm.getMaterialImages10())):"";
					
					bo.setMaterialImages1(photo1);
					bo.setMaterialImages2(photo2);
					bo.setMaterialImages3(photo3);
					bo.setMaterialImages4(photo4);
					bo.setMaterialImages5(photo5);
					bo.setMaterialImages6(photo6);
					bo.setMaterialImages7(photo7);
					bo.setMaterialImages8(photo8);
					bo.setMaterialImages9(photo9);
					bo.setMaterialImages10(photo10);
					bo.setVendorId(inputForm.getVendorId());
					bo.setBrandId(inputForm.getBrandId());
					bo.setCarStyleId(inputForm.getCarStyleId());
					bo.setCarName(inputForm.getCarName());
					bo.setIsMatch(1);
					bo.setMarketNo(TmsConstants.MARKET_NO);
					WeChatMqApi.getInstance().call(bo);
				} 
			}else if ("3".equals(inputForm.getSaveInputInfo())) {// update load pic action/ Old logical code
				String serial = (String) uc.getAttribute("serial");
				String localIp = (String)uc.getAttribute("localIp");
				String picPoint = (String)uc.getAttribute("picPoint");
				uc.removeAttribute("serial");
				uc.removeAttribute("sysPath");
				uc.removeAttribute("localIp");
				uc.removeAttribute("picPoint");
				if (serial != null && !"".equals(serial)) {
				String path = "smb://" + localIp + "/1/";
				SmbFile file = new SmbFile(path);
				if (!file.exists()) {
					logger.error("carRgInputConfirmController path error,url = "+path);
					return;
				} 
				
				controller.begin();
				if (file.isDirectory()) {
					String[] filelist = file.list();
					String carInfoId = "";
					TransferAcceptedInfo accept = new TransferAcceptedInfo();
					accept = accept.findTransStatusBySerial(serial);
					String sortTime ="";
					List list = new ArrayList();
					for (int i = 0; i < filelist.length; i++) {
						SmbFile readfile = new SmbFile(path + "" + filelist[i]);
						if (!readfile.isDirectory()) {
							long a = readfile.lastModified();
							sortTime += String.valueOf(a)+",";
							String name[] = filelist[i].split("=");
							carInfoId = name[0];
							Map map = new HashMap();
							map.put(String.valueOf(a), carInfoId);
							list.add(map);
						} 
					}
					File outFile = null;
					SmbFile inFile = null;
					List realList = new ArrayList();
					if(sortTime.length() > 0)
					{
						String[] bb = sortTime.split(",");
						Arrays.sort(bb); 
						List yyList = Arrays.asList(bb);	
						for(int i = 0;i<yyList.size();i++)
						{
							String sunxu = (String)yyList.get(i);
							for(int j=0;j<list.size();j++)
							{
								Map nameMap = (Map)list.get(j);
								String nameVa = (String)nameMap.get(sunxu);
								if(nameVa!=null)
								{
									Map realMap = new HashMap();
									realMap.put(i, nameVa);
									realList.add(realMap);
								}
							}
						}
						if (realList != null && realList.size() > 0) {
							for (int p = 0; p < realList.size(); p++) {
								Map map = (Map) realList.get(p);
								String cid = (String) map.get(p);
								if(StringUtil.isNotBlank(cid) && cid.indexOf("jpg")!=-1)
								{
									String picIndex = "0";
									if(StringUtil.isNotBlank(picPoint))
									{
										TransferRgInputViewHelper view = (TransferRgInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
										if("1".equals(picPoint))
										{
											picIndex = "2";
											String url = imgPath(serial,"2");
											accept.setMaterialImages1(url);
											view.setMaterialImages1(url);
											
										}
										if("2".equals(picPoint))
										{
											picIndex = "3";
											String url = imgPath(serial,"3");
											accept.setMaterialImages2(url);
											view.setMaterialImages2(url);
										}
										if("3".equals(picPoint))
										{
											picIndex = "4";
											String url = imgPath(serial,"4");
											accept.setMaterialImages3(url);
											view.setMaterialImages3(url);
										}
										if("4".equals(picPoint))
										{
											picIndex = "5";
											String url = imgPath(serial,"5");
											accept.setMaterialImages4(url);
											view.setMaterialImages4(url);
										}
										if("5".equals(picPoint))
										{
											picIndex = "6";
											String url = imgPath(serial,"6");
											accept.setMaterialImages5(url);
											view.setMaterialImages5(url);
										}
										if("6".equals(picPoint))
										{
											picIndex = "7";
											String url = imgPath(serial,"7");
											accept.setMaterialImages6(url);
											view.setMaterialImages6(url);
										}
										if("7".equals(picPoint))
										{
											picIndex = "8";
											String url = imgPath(serial,"8");
											accept.setMaterialImages7(url);
											view.setMaterialImages7(url);
										}
										if("8".equals(picPoint))
										{
											picIndex = "9";
											String url = imgPath(serial,"9");
											accept.setMaterialImages8(url);
											view.setMaterialImages8(url);
										}
										if("9".equals(picPoint))
										{
											picIndex = "10";
											String url = imgPath(serial,"10");
											accept.setMaterialImages9(url);
											view.setMaterialImages9(url);
										}if("10".equals(picPoint))
										{
											picIndex = "11";
											String url = imgPath(serial,"11");
											accept.setMaterialImages10(url);
											view.setMaterialImages10(url);
										}
										inFile = new SmbFile(path + "" + cid);
										outFile = new File(ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY) + serial + "/") + serial + "_"+ picIndex + ".jpg");
										CopyFile.copy2(inFile, outFile);
										inFile.delete();//
									}
									accept.setSerial(serial);
									accept.updatePic(accept);
								}
							}
						}
					}
				  }
				}	
				controller.commit();
			}
		} catch (Exception e) {
			logger.error("CarRgInputConfirmController exception", e);
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");

	}

	private TransferAcceptedInfoDto convert(TransferRgActionForm inputForm, UserContext uc) throws TmsDaoException {

		TransferAcceptedInfoDto r = new TransferAcceptedInfoDto();
		r.setChangeBody(Integer.parseInt(inputForm.getChangeBody()));
		r.setSerial(inputForm.getSerial());
		r.setVin(inputForm.getVin());
		r.setVendorCode(inputForm.getVendorCode());
		r.setModelCode(inputForm.getModelCode());
		if (StringUtil.isNotBlank(inputForm.getCardType()))
			r.setCy_cardType(Integer.parseInt(inputForm.getCardType()));
		r.setCountryStyle(inputForm.getCountryStyle());
		r.setCarUse(inputForm.getCarUse());
		r.setCarType(inputForm.getCarType());
		r.setRegisterCardNo(inputForm.getRegisterCardNo());
		if (StringUtil.isNotBlank(inputForm.getRegDate()))
			r.setRegDate(DateUtil.getStringToTimeStamp1(inputForm.getRegDate() + " 00:00:00"));
		if (StringUtil.isNotBlank(inputForm.getGainType()))
			r.setGainType(Integer.parseInt(inputForm.getGainType()));
		MarketCarInfoDto info = Car.getInstance().getCtiyInfo(inputForm.getCarMarketIn());
		if (info != null) {
			r.setCarMarketInProv(Integer.parseInt(info.getPrivilId()));
			r.setCarMarketInCity(Integer.parseInt(info.getCityId()));
		}
		if (StringUtil.isNotBlank(inputForm.getPrice()))
			r.setPrice(Integer.parseInt(StringUtil.removeDH(inputForm.getPrice())));
		if (inputForm.getCarOwner().length() > 5) {
			r.setOwnerType(2);
			r.setOwnerOrgNo(inputForm.getIdentification());
		} else {
			r.setOwnerType(1);
			r.setCarOwnerIdCardNo(inputForm.getIdentification());
		}

		if (inputForm.getNewCarOwner().length() > 5) {
			r.setNewOwnerType(2);
			r.setNewOwnerOrgNo(inputForm.getNewIdentification());
		} else {
			r.setNewOwnerType(1);
			r.setNewcarOwnerIdCardNo(inputForm.getNewIdentification());
		}
		r.setNewcarOwner(StringUtil.formatCarName(inputForm.getNewCarOwner()));
		r.setCarOwner(StringUtil.formatCarName(inputForm.getCarOwner()));
		r.setOwnerPhone(inputForm.getOwnerPhone());
		r.setOwnerAddress(inputForm.getOwnerAddress());
		r.setAgentOwnerIdCardNo(inputForm.getAgentOwnerIdCardNo());
		r.setAgentName(StringUtil.formatCarName(inputForm.getAgentName()));
		r.setAgentPhone(inputForm.getAgentPhone());
		r.setAgentAddress(inputForm.getAgentAddress());
		r.setNewOwnerPhone(inputForm.getNewOwnerPhone());
		r.setNewOwnerAddress(inputForm.getNewOwnerAddress());
		r.setNewOwnerZipCode(inputForm.getZipCode());
		r.setNewAgentOwnerIdCardNo(inputForm.getNewAgentOwnerIdCardNo());
		r.setNewAgentName(StringUtil.formatCarName(inputForm.getNewAgentName()));
		r.setNewAgentPhone(inputForm.getNewAgentPhone());
		r.setNewAgentAddress(inputForm.getNewAgentAddress());
		r.setMaterialImages1(inputForm.getMaterialImages1());
		r.setMaterialImages2(inputForm.getMaterialImages2());
		r.setMaterialImages3(inputForm.getMaterialImages3());
		r.setMaterialImages4(inputForm.getMaterialImages4());
		r.setMaterialImages5(inputForm.getMaterialImages5());
		r.setMaterialImages6(inputForm.getMaterialImages6());
		r.setMaterialImages7(inputForm.getMaterialImages7());
		r.setMaterialImages8(inputForm.getMaterialImages8());
		r.setMaterialImages9(inputForm.getMaterialImages9());
		r.setMaterialImages10(inputForm.getMaterialImages10());
		if (StringUtils.isNotBlank(inputForm.getTransfer()))
			r.setTransfer(Integer.parseInt(inputForm.getTransfer()));
		else
			r.setTransfer(2);
		if (StringUtils.isNotBlank(inputForm.getTransferRegister()))
			r.setTransferRegister(Integer.parseInt(inputForm.getTransferRegister()));
		else
			r.setTransferRegister(2);
		TmsUserProfile profile = uc.getUserProfile();
		r.setInsertUserid(profile.getUniqueId());
		r.setZy_printnum(1);
		// r.setZy_printTime(inputForm.getzyPrintTime);
		//
		TransferAcceptedInfo dto = TransferAcceptedInfo.getInstance().getAcceptInfoBySerial(inputForm.getSerial(),
				null);
		if (dto != null) {
			if (dto.getStatus() == 1)
				r.setStatus(2);
			else
				r.setStatus(dto.getStatus());
		} else {
			r.setStatus(1);
		}

		if ("1".equals(inputForm.getSaveInputInfo())) {
			if ("1".equals(inputForm.getSpecialProcess())) {
				r.setTransfer(dto.getTransfer());
				r.setTransferRegister(dto.getTransferRegister());
			} else {
				r.setTransfer(1);
				r.setTransferRegister(1);
			}
		}

		r.setZyrintUserId(profile.getUniqueId() + "");
		r.setCarName(inputForm.getCarName());
		TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance().getAcceptInfoBySerial(inputForm.getSerial(), null, null);
		if (StringUtil.isNotBlank(inputForm.getVin()) && 
				(transferAcceptedInfo == null || !inputForm.getVin().equals(transferAcceptedInfo.getVin()))) {
			String modelId = TmsCarModelInfoApi.getInstance().getCarModelInfoByVin(inputForm.getVin());
			if (!StringUtil.isNullOrBlank(modelId)) {
				CarStyle carStyle = CarStyle.getInstance();
				carStyle = carStyle.findCarStyleInfo(modelId);
				if (!StringUtil.isNullOrBlank(carStyle.getCarStyleId())) {
					r.setIsMatch(2);
					r.setVendorId(carStyle.getVendorId());
					r.setCarName(carStyle.getCarStyleName());
					r.setBrandId(carStyle.getBrandId());
					r.setCarStyleId(carStyle.getCarStyleId());
				}
			}
		}
		return r;
	}

	/**
	 * 处理存入数据库中的图片路径
	 */
	private String imgPath(String serial, String imgKey) {
		return ImageUtils.getImageUrl(
				Registry.getConfig(TmsConstants.UPLOAD_ZL_IMAGE_KEY) + serial + "/" + serial + "_" + imgKey + ".jpg");
	}
	
	/**
	 * 图片名称的取得
	 * 
	 * @param imagePath 图片路径
	 * @return
	 */
	private String getImageName(String imagePath){
		if(StringUtil.isNullOrBlank(imagePath)) return "";
		return imagePath.substring(imagePath.lastIndexOf("/")+1);
	}
}
